/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.evswork.getwit.regexp.RegExpManager;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;

public class TweetListAdapter extends ArrayAdapter<TweetObject> implements OnClickListener {
	public final static int LIMIT = 70;
	public final static int FLEX = 10;
	
	public TweetListAdapter(Context context) {
		super(context,0);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TweetView newView = new TweetView(getContext());
		TweetObject item = this.getItem(position);
		newView.update(item);
		newView.setTag(Integer.valueOf(position));
		Pattern pattern = Pattern.compile(
			"(?:用户个人信息\n\n用户名： |User Profile:\n\nUsername: )([_0-9a-zA-Z]+)\n\n",
			Pattern.DOTALL);
		Matcher matcher = pattern.matcher(newView.data.content);
		boolean flag = (newView.data.flag != TweetObject.NOT_TWEET) || (matcher.find());
		if (flag) {
			newView.setOnClickListener(this);
		}
		if (RegExpManager.getInstance(getContext()).match(item.content))
		{
			newView.UserText.setText("[被屏蔽消息]");
			newView.MsgText.setText("[消息已屏蔽]");
			newView.setOnClickListener(null);
		}
		
		return newView;
	}
	
	public void postRawMessage(String rawdata) {
		markTweetsOld();
		
		int MsgCount=0;
		Pattern pattern = Pattern.compile(
			"\\*(?:\\d+\\) )?([_0-9a-zA-Z]+):\\* (.*?)\n\\[ ID:  (\\d*) \\]",
			Pattern.DOTALL
		);
		Matcher m=pattern.matcher(rawdata);
		while(m.find()) {
			++MsgCount;
			this.add(
				new TweetObject(getContext(),
					m.group(2), m.group(1), Long.valueOf(m.group(3))
				)
			);
		}
		if (MsgCount == 0) {
			this.add(new TweetObject(getContext(),rawdata.trim(),"[信息]",0));
		}
	}

	private void markTweetsOld()
	{
		int count = this.getCount();
		int i;
		for (i=0;i<count;++i)
		{
			this.getItem(i).recent = false;
		}
	}
	
	@Override
	public void add(TweetObject object) {
		// add items at top
		if (this.getPosition(object) != -1)
			this.remove(object);
		super.insert(object,0);
		trimView();
	}

	private void trimView() {
		if ( this.getCount() < LIMIT) {
			return;
		}
		//Log.d("Getwit",String.format("trim start,count=%d", getCount()));
		for (int i=0;i<FLEX;++i) {	
			this.remove(this.getItem(this.getCount()-1));
		}
		//Log.d("Getwit",String.format("trim end,count=%d", getCount()));
	}
	
	@Override
	public void onClick(View v) {
		int position = (Integer)v.getTag();
		((GttMainActivity)getContext()).onTweetSelected(getItem(position));
	}

	
}
